/**
 * Copyright (c) 2012-2013 Yunzhisheng(Shanghai) Co.Ltd. All right reserved.
 * @FileName : MainActivity.java
 * @ProjectName : letvAssistant
 * @PakageName : cn.yunzhisheng.voicetv
 * @Author : Brant
 * @CreateDate : 2013-5-22
 */
package cn.yunzhisheng.vui.assistant.tv;

import cn.yunzhisheng.vui.assistant.preference.SessionPreferenceOfIntent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;

/**
 * @Module : 隶属模块名
 * @Comments : 描述
 * @Author : Brant
 * @CreateDate : 2013-5-22
 * @ModifiedBy : Brant
 * @ModifiedDate: 2013-5-22
 * @Modified: 2013-5-22: 实现基本功能
 */
public class VuiActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent i = new Intent(this, WindowService.class);
        startService(i);

        Intent vritualKey = new Intent(MessageReceiver.ACTION_VIRTUAL_KEY_SERVER);
        startService(vritualKey);

        mVuiHandle = new VuiHandle(this, getWindow().getDecorView());

    }

    protected VuiHandle mVuiHandle;

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        try {
            if (hasFocus) {
                mVuiHandle.registerReceiver();
            } else {
                mVuiHandle.unregisterReceiver();
            }
        } catch (Exception e) {

        }
        super.onWindowFocusChanged(hasFocus);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == 141) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                sendBroadcast(new Intent(SessionPreferenceOfIntent.ACT_RECOGNIZE_OPERATE_START_TALK_TO_TV));
            }
        } else if (event.getKeyCode() == 142) {
            if (event.getAction() == KeyEvent.ACTION_UP) {
                sendBroadcast(new Intent(SessionPreferenceOfIntent.ACT_RECOGNIZE_OPERATE_STOP_TALK_TO_TV));
            }
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

}
